package com.jxmcloud.business.cut.service.impl.app.activity;

import cn.hutool.core.util.ObjectUtil;
import com.jxmcloud.business.cut.constants.CutConstants;
import com.jxmcloud.business.cut.constants.CutErrorMsg;
import com.jxmcloud.business.cut.dto.client.DrawLogListDto;
import com.jxmcloud.business.cut.dto.client.DrawSpecialTurntableAwardDto;
import com.jxmcloud.business.cut.dto.client.DrawTurntableAwardDto;
import com.jxmcloud.business.cut.dto.client.GetTurntableInfoDto;
import com.jxmcloud.business.cut.dtoCommon.CashDetailInfoDto;
import com.jxmcloud.business.cut.dtoCommon.TurntableDrawLog;
import com.jxmcloud.business.cut.dtoCommon.TurntableInfo;
import com.jxmcloud.business.cut.json.DataConfigService;
import com.jxmcloud.business.cut.model.database.DBUserTurntableLog;
import com.jxmcloud.business.cut.service.app.activity.ITurntableService;
import com.jxmcloud.business.cut.service.database.IUserTurntableLogDBService;
import com.jxmcloud.business.cut.service.user.*;
import com.jxmcloud.business.cut.util.MyTool;
import com.jxmcloud.business.cut.vo.client.DrawLogListVo;
import com.jxmcloud.business.cut.vo.client.DrawSpecialTurntableAwardVo;
import com.jxmcloud.business.cut.vo.client.DrawTurntableAwardVo;
import com.jxmcloud.business.cut.vo.client.GetTurntableInfoVo;
import com.jxmcloud.business.cut.vo.json.ConfigAwardVo;
import com.jxmcloud.business.cut.vo.user.PlayerInfoVo;
import com.jxmcloud.business.cut.vo.user.UserCashDetailItemVo;
import com.jxmcloud.business.cut.vo.user.UserTurnItem;
import com.xlcloud.platform.api.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TurntableServiceImpl implements ITurntableService {
    @Resource
    private DataConfigService dataConfigService;
    @Resource
    private IUserDrawTurnService userDrawTurnService;
    @Resource
    private IUserFlowService userFlowService;
    @Resource
    private IUserBagService userBagService;
    @Resource
    private IUserCashDetailListService userCashDetailListService;
    @Resource
    private IUserActivityService userActivityService;
    @Resource
    private IUserTurntableLogDBService userTurntableLogDBService;


    @Override
    public DrawSpecialTurntableAwardVo drawSpecialTurntableAward(DrawSpecialTurntableAwardDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_TRUE;
        DrawSpecialTurntableAwardVo ansData = new DrawSpecialTurntableAwardVo();
        do {
            // 扣除道具数量
            boolean itemEnough = userBagService.delLotteryItem(player);
            if (!itemEnough) {
                error = CutErrorMsg.ERROR_ITEM_NOT_ENOUGH;
                break;
            }
            // 获取转盘奖励
            UserTurnItem awardInfo = userDrawTurnService.startSpecialTurnTable(player);
            if (ObjectUtil.isNull(awardInfo)) {
                error = CutErrorMsg.ERROR_NO_AWARD;
                break;
            }
            // 转换奖励
            userDrawTurnService.updatePlayerData(player, awardInfo, false);
            //
            ConfigAwardVo awardConfig = dataConfigService.getConfigAwardConfig(awardInfo.getAwardId());
            if (ObjectUtil.isNull(awardConfig)) {
                error = CutErrorMsg.ERROR_FALSE;
                break;
            }
            //
            Integer nextProgress = awardConfig.getProgressId();
            userFlowService.enterNextProgress(player, nextProgress);
            //  回包处理
            ansData.setFlowId(nextProgress);
            ansData.setBagInfo(userBagService.packBagData(player));
            ansData.setFitId(awardInfo.getAwardId());

            // 记录日志
            DBUserTurntableLog dbUserTurntableLog = new DBUserTurntableLog();
            dbUserTurntableLog.setUid(player.getBase().getUid());
            dbUserTurntableLog.setActivityId(player.getActivity().getActivityId());
            dbUserTurntableLog.setPoolId(awardConfig.getPoolId());
            dbUserTurntableLog.setAwardId(awardInfo.getAwardId());
            dbUserTurntableLog.setItemId(0);
            dbUserTurntableLog.setItemCount(0);
            dbUserTurntableLog.setAddTime(new Date());

            // 获取零钱的记录信息
            UserCashDetailItemVo cashDetail = userCashDetailListService.getCashDetailItemByProgressId(player, nextProgress);
            if (ObjectUtil.isNotNull(cashDetail)) {
                CashDetailInfoDto detailData = userCashDetailListService.packVotoDto(cashDetail);
                ansData.setCashDetail(detailData);
                //
                dbUserTurntableLog.setItemId(cashDetail.getId());
                dbUserTurntableLog.setItemCount(cashDetail.getCount());
            }

            userTurntableLogDBService.insertData(dbUserTurntableLog);

        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return ansData;
    }

    /**
     * 转盘抽奖 返回 对应条件的转盘
     *
     * @param dto    入参
     * @param player 用户存储的信息
     * @return 转盘数据
     */
    @Override
    public DrawTurntableAwardVo drawTurntableAward(DrawTurntableAwardDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_TRUE;
        DrawTurntableAwardVo ansData = new DrawTurntableAwardVo();
        do {
            // 扣除道具数量
            boolean itemEnough = userBagService.delLotteryItem(player);
            if (!itemEnough) {
                error = CutErrorMsg.ERROR_ITEM_NOT_ENOUGH;
                break;
            }

            // 获取转盘奖励
            UserTurnItem awardInfo = userDrawTurnService.startTurnTable(player);
            if (ObjectUtil.isNull(awardInfo)) {
                error = CutErrorMsg.ERROR_NO_AWARD;
                break;
            }

            // 转换奖励
            userDrawTurnService.updatePlayerData(player, awardInfo, true);
            //
            ConfigAwardVo awardConfig = dataConfigService.getConfigAwardConfig(awardInfo.getAwardId());
            if (ObjectUtil.isNull(awardConfig)) {
                error = CutErrorMsg.ERROR_FALSE;
                break;
            }
            TurntableInfo packData = userDrawTurnService.packTurnTableData(player);
            if (ObjectUtil.isNull(packData)) {
                error = CutErrorMsg.ERROR_FALSE;
                break;
            }
            //
            Integer nextProgress = awardConfig.getProgressId();
            userFlowService.enterNextProgress(player, nextProgress);

            //  回包处理
            ansData.setFlowId(nextProgress);
            ansData.setBagInfo(userBagService.packBagData(player));
            ansData.setFitId(awardInfo.getAwardId());
            ansData.setInfo(packData);

            // 记录日志
            DBUserTurntableLog dbUserTurntableLog = new DBUserTurntableLog();
            dbUserTurntableLog.setUid(player.getBase().getUid());
            dbUserTurntableLog.setActivityId(player.getActivity().getActivityId());
            dbUserTurntableLog.setPoolId(awardConfig.getPoolId());
            dbUserTurntableLog.setAwardId(awardInfo.getAwardId());
            dbUserTurntableLog.setItemId(0);
            dbUserTurntableLog.setItemCount(0);
            dbUserTurntableLog.setAddTime(new Date());

            // 获取零钱的记录信息
            UserCashDetailItemVo cashDetail = userCashDetailListService.getCashDetailItemByProgressId(player, nextProgress);
            if (ObjectUtil.isNotNull(cashDetail)) {
                CashDetailInfoDto detailData = userCashDetailListService.packVotoDto(cashDetail);
                ansData.setCashDetail(detailData);
                //
                dbUserTurntableLog.setItemId(cashDetail.getId());
                dbUserTurntableLog.setItemCount(cashDetail.getCount());
            }

            userTurntableLogDBService.insertData(dbUserTurntableLog);

        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return ansData;
    }


    // 获取转盘信息
    @Override
    public GetTurntableInfoVo getTurntableInfo(GetTurntableInfoDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_TRUE;
        GetTurntableInfoVo ansData = new GetTurntableInfoVo();
        do {
            TurntableInfo packData = userDrawTurnService.packTurnTableData(player);
            if (ObjectUtil.isNull(packData)) {
                error = CutErrorMsg.ERROR_FALSE;
                break;
            }
            ansData.setInfo(packData);
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return ansData;
    }

    @Override
    public DrawLogListVo drawLogList(DrawLogListDto dto, PlayerInfoVo player) {
        String error = CutErrorMsg.ERROR_TRUE;
        DrawLogListVo ansData = new DrawLogListVo();
        do {
            Integer uid = player.getBase().getUid();
            Integer activityId = player.getActivity().getActivityId();
            List<DBUserTurntableLog> daoList = userTurntableLogDBService.selectLastN(uid, activityId, CutConstants.LOG_COUNT_TURNTABLE);
            List<TurntableDrawLog> list = Lists.newArrayList();
            for (DBUserTurntableLog log : daoList) {
                TurntableDrawLog logData = new TurntableDrawLog();
                logData.setId(log.getAwardId());
                logData.setItemId(log.getItemId());
                logData.setItemCount(log.getItemCount());
                Long addTime = MyTool.toTimeByDate(log.getAddTime());
                logData.setTime(addTime.intValue());
                list.add(logData);
            }
            ansData.setList(list);
        } while (false);
        if (!error.equals(CutErrorMsg.ERROR_TRUE)) {
            // 抛出异常
            throw new BusinessException(error);
        }
        return ansData;
    }
}
